# Read the file from the specified path
file_path = r'D:\VPN\hr5s.txt'
file_path2 = r'D:\VPN\hr1s.txt'
hr1s=[]
hr5s=[]
# Open the file and read it line by line
with open(file_path2, 'r', encoding='utf-8') as file:
    for line in file:
        # print(line.strip())  # strip() is used to remove any leading/trailing whitespace or newline characters
        hr1s.append(line.strip())
hr1slist=[int(i) for i in hr1s]
hr1slist.reverse()
with open(file_path, 'r', encoding='utf-8') as file:
    for line in file:
        # print(line.strip())  # strip() is used to remove any leading/trailing whitespace or newline characters
        hr5s.append(line.strip())

hr5slist=[int(i) for i in hr5s]
print(hr1slist)
print(hr5slist)

import numpy as np
import matplotlib.pyplot as plt
from scipy.signal import butter, filtfilt

# 假设 hr2 是你的数据列表
# hr2 = [....]  # 请替换成实际数据

# 设计低通滤波器
def butter_lowpass(cutoff, fs, order=5):
    nyquist = 0.5 * fs  # 奈奎斯特频率
    normal_cutoff = cutoff / nyquist  # 正常化截止频率
    b, a = butter(order, normal_cutoff, btype='low', analog=False)
    return b, a

# 应用滤波器
def apply_lowpass_filter(data, cutoff, fs, order=5):
    b, a = butter_lowpass(cutoff, fs, order=order)
    y = filtfilt(b, a, data)
    return y

# 示例滤波参数
fs = 100.0  # 采样频率 (Hz)
cutoff = 2.0  # 截止频率 (Hz)

# 应用滤波器
hr2_filtered = apply_lowpass_filter(hr5slist, cutoff, fs)

# 绘图
plt.figure(figsize=(10, 6))
plt.plot(hr5slist, label='Original Data', color='blue')
plt.plot(hr2_filtered, label='Filtered Data', color='red', linestyle='--')
plt.plot(hr1slist, label='Filtered Data', color='green', linestyle='--')
plt.title('HR2 Data - Original vs Filtered')
plt.xlabel('Sample Index')
plt.ylabel('Amplitude')
plt.legend()
plt.grid()
plt.show()
